package com.shuyu.flutter_plugin_platfromview;


import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

import java.util.Map;

import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;

public class FlutterActivityIndicator implements PlatformView, MethodCallHandler {
    private final ProgressBar progressBar;
    private final MethodChannel methodChannel;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @SuppressWarnings("unchecked")
    FlutterActivityIndicator(
            final Context context,
            BinaryMessenger messenger,
            int id,
            Map<String, Object> params,
            final View containerView) {

        Log.i("initParams", params.get("initParams").toString());
        progressBar = new ProgressBar(context);

        methodChannel = new MethodChannel(messenger, "plugins/platforview_indicator" + id);
        methodChannel.setMethodCallHandler(this);

    }

    @Override
    public View getView() {
        return progressBar;
    }

    @Override
    public void onMethodCall(MethodCall methodCall, Result result) {
        switch (methodCall.method) {
            case "start":
                progressBar.setVisibility(View.VISIBLE);
                break;
            case "stop":
                progressBar.setVisibility(View.INVISIBLE);
                break;
        }
    }

    @Override
    public void dispose() {
        methodChannel.setMethodCallHandler(null);
        progressBar.setVisibility(View.GONE);
    }
}